﻿#define  _CRT_SECURE_NO_WARNINGS 1

#include "Snake.h"

//设置光标位置
void SetPos(int x, int y)
{
	COORD pos = { x,y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, pos);
}

//欢迎界面
void WelcomeScreen()
{
	SetPos(40, 13);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(41, 24);
	system("pause");
	SetPos(26, 13);
	printf("请使用↑.↓.←.→.按键控制方向, F3加速, F4减速");
	SetPos(41, 24);
	system("pause");
}

//活动范围设置
void Range()
{
	system("cls");
	setlocale(LC_ALL, "");
	for (int i = 0; i <= 56; i += 2)
	{
		SetPos(i, 0);
		wprintf(L"%c", WALL);
		SetPos(i, 26);
		wprintf(L"%c", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
		SetPos(56, i);
		wprintf(L"%c", WALL);
	}
}

//帮助信息
void HelpMessage()
{
	SetPos(65, 15);
	printf("不能咬到自己");
	SetPos(65, 16);
	printf("使用↑.↓.←.→.按键控制方向");
	SetPos(65, 17);
	printf("F3加速, F4减速,加速获分增加");
	SetPos(65, 18);
	printf("ESC：退出游戏，空格：暂停游戏");
}



//初始化蛇身
void InitSnake(pSnake ps)
{
	ps->_pSnake = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (ps->_pSnake == NULL)
	{
		perror("InitSnake():malloc");
		return;
	}
	ps->_pSnake->x = COLINIT;
	ps->_pSnake->y = ROWINIT;
	ps->_pSnake->next = NULL;

	pSnakeNode p;
	for (int i = 1; i < 5; i++)
	{
		p = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (p == NULL)
		{
			perror("InitSnake():malloc");
			return;
		}
		p->x = i * 2 + COLINIT;
		p->y = ROWINIT;
		p->next = ps->_pSnake;
		ps->_pSnake = p;
	}
	ps->_Sta = OK;
	ps->_SleepTime = 200;
	ps->_FoodWeight = 10;
	ps->_Socre = 0;
	ps->_Dir = RIGHT;
	p = ps->_pSnake;
	while (p != NULL)
	{
		SetPos(p->x, p->y);
		wprintf(L"%c", SNAKE);
		p = p->next;
	}
}

//食物创建
void CreateFood(pSnake ps)
{
	assert(ps != NULL);
	int x;
	int y;
	pSnakeNode p;
	agin:
	p = ps->_pSnake;
	do 
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	while (p != NULL)
	{
		if (p->x == x && p->y == y)
		{
			goto agin;
		}
		p = p->next;
	}
	pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (food == NULL)
	{
		perror("CreateFood():malloc");
		return;
	}
	food->x = x;
	food->y = y;
	food->next = NULL;
	ps->_Food = food;
	SetPos(x, y);
	wprintf(L"%c", FOOD);
}

//游戏初始化
void GameInit(pSnake ps)
{
	
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOutput, &CursorInfo);
	WelcomeScreen();
	Range();
	HelpMessage();
	InitSnake(ps);
	CreateFood(ps);
}

//暂停游戏
void SPACETIME()
{
	while (1)
	{
		if (KEY_PRESS(VK_SPACE))
		{
			return;
		}
	}
}

//吃食物
void EatFood(pSnake ps)
{
	ps->_Socre += ps->_FoodWeight;
	CreateFood(ps);
}

//没有食物
void NotFood(pSnake ps)
{
	pSnakeNode p;
	p = ps->_pSnake;
	/**/
	while (p->next->next != NULL)
	{
		p = p->next;
	}
	SetPos(p->next->x, p->next->y);
	printf("%s", "  ");
	pSnakeNode tmp;
	tmp = p->next;
	p->next = p->next->next;
	free(tmp);
	tmp = NULL;
}

//判断是否死亡
void Kill(pSnake ps)
{
	if ((ps->_pSnake->x <= 0) || (ps->_pSnake->x >= 56) || (ps->_pSnake->y <= 0) || (ps->_pSnake->y >= 26))
	{
		ps->_Sta = KILL_BY_WALL;
	}
	pSnakeNode p;
	p = ps->_pSnake->next;
	while (p != NULL)
	{
		if ((p->x == ps->_pSnake->x) && (p->y == ps->_pSnake->y))
		{
			ps->_Sta = KILL_BY_SELF;
			return;
		}
		p = p->next;
	}
}

//蛇移动与食物刷新
void SnakeMove(pSnake ps)
{
	int x = 0;
	int y = 0;
	if (ps->_Dir == UP)
	{
		if (ps->_pSnake->y <= UPMAX)
		{
			y = DOWNMAX;
			x = ps->_pSnake->x;
		}
		else
		{
			x = ps->_pSnake->x;
			y = ps->_pSnake->y - 1;
		}
		
	}
	else if (ps->_Dir == DOWN)
	{
		if (ps->_pSnake->y >= DOWNMAX)
		{
			x = ps->_pSnake->x;
			y = UPMAX;
		}
		else
		{
			x = ps->_pSnake->x;
			y = ps->_pSnake->y + 1;
		}
	}
	else if (ps->_Dir == LEFT)
	{
		if (ps->_pSnake->x <= LEFTMAX)
		{
			x = RIGHTMAX;
			y = ps->_pSnake->y;
		}
		else
		{
			x = ps->_pSnake->x - 2;
			y = ps->_pSnake->y;
		}
		
	}
	else if (ps->_Dir == RIGHT)
	{
		if (ps->_pSnake->x >= RIGHTMAX)
		{
			x = LEFTMAX;
			y = ps->_pSnake->y;
		}
		else
		{
			x = ps->_pSnake->x + 2;
			y = ps->_pSnake->y;
		}
		
	}
	pSnakeNode p = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (p == NULL)
	{
		perror("SnakeMove():malloc");
		return;
	}
	p->next = ps->_pSnake;
	p->x = x;
	p->y = y;
	ps->_pSnake = p;
	SetPos(p->x, p->y);
	wprintf(L"%c", SNAKEHEAD);
	p = p->next;
	while (p != NULL)
	{
		SetPos(p->x, p->y);
		wprintf(L"%c", SNAKE);
		p = p->next;
	}
	if ((ps->_pSnake->x == ps->_Food->x) && ((ps->_pSnake->y == ps->_Food->y)))
	{
		EatFood(ps);
	}
	else
	{
		NotFood(ps);
	}
	Kill(ps);
}

//游戏运行
void GameRun(pSnake ps)
{
	do
	{
		SetPos(65, 10);
		printf("现在总分为：%d", ps->_Socre);
		SetPos(65, 11);
		printf("每一颗食物分数为：%d", ps->_FoodWeight);
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_FoodWeight < 34)
			{
				ps->_SleepTime -= 20;
				ps->_FoodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_FoodWeight > 10)
			{
				ps->_SleepTime += 20;
				ps->_FoodWeight -= 2;
			}
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			SPACETIME();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Sta = END_ES;
			break;
		}
		SnakeMove(ps);
		Sleep(ps->_SleepTime);
		
	} while (ps->_Sta == OK);
}

//游戏结束
void GameEnd(pSnake ps)
{
	pSnakeNode p = ps->_pSnake->next;
	while (p != NULL)
	{
		ps->_pSnake->next = p->next;
		free(p);
		p = ps->_pSnake->next;
	}
	free(ps->_pSnake);
	ps->_pSnake = NULL;
}
